<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018/8/23 0023
 * Time: 11:11
 */
namespace app\admin\model;
use think\Db;

class CompanyModel
{
    /**子公司查询模型
     * @return \think\db\Query
     */
    static public function getCompany(){
        return Db::name('company');
    }

    /**添加公司
     * @param $data
     * @return int|string
     */
    static public function addCompany($data){
        return Db::name('company')->insert($data);
    }

    /**删除公司
     * @param $field
     * @param null $op
     * @param null $condition
     * @return int
     */
    static public function delCompany($field, $op = null, $condition = null){
        $sul = Db::connect();
        $companys = $sul->name('company')->where($field,$op,$condition)->select();
        foreach ($companys as $company){
            $id = $company['id'];
            $sul->name('departmen_table')->where('company_id',$id)->delete();
            $sul->name('position_table')->where('company_id',$id)->delete();
            $sul->name('card_setting')->where('company_id',$id)->delete();
            $sul->name('leave')->where('company_id',$id)->delete();
            $sul->name('punch_clock')->where('company_id',$id)->delete();
            $user = $sul->name('user')->where('company_id',$id)->select();
            foreach ($user as $value){
                if($value['portrait']){
                    if(is_file(config('FileRoot').$value['portrait'])){
                        unlink(config('FileRoot').$value['portrait']);
                    }
                }
            }
            $sul->name('user')->where('company_id',$id)->delete();
            $sul->name('work_report')->where('company_id',$id)->delete();
            $sul->name('integral_change')->where('company_id',$id)->delete();
            $sul->name('post_release')->where('company_id',$id)->delete();
            $sul->name('comment')->where('company_id',$id)->delete();
        }
        return $sul->name('company')->where($field,$op,$condition)->delete();
    }

    /**更新公司
     * @param $data
     * @return int|string
     */
    static public function updateCompany($data){
        return Db::name('company')->update($data);
    }
}